package edu.zhangxu.servlet;

import edu.zhangxu.dao.SolutionDao;
import edu.zhangxu.dao.SolutionDaoImp;
import edu.zhangxu.pojo.Solution;
import edu.zhangxu.util.JDBCUtils;
import jdk.nashorn.internal.scripts.JD;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class QueryResult extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id = Integer.parseInt(req.getParameter("id"));
        resp.setContentType("text/html;charset=utf-8");
        resp.setHeader("Access-Control-Allow-Origin", "*"); // 允许跨域访问
        Connection con = null;
        Solution solution = null;
        SolutionDao dao = new SolutionDaoImp();
        JSONObject jo = null;
        try {
            con = JDBCUtils.getConnection();
            solution = dao.getSimpleSolutionById(con, id);
            jo = JSONObject.fromObject(solution);
//            System.out.println(jo);
        } catch (Exception e) {
            e.printStackTrace();
            jo = JSONObject.fromObject(e);
        } finally {
            resp.getWriter().print(jo);
            JDBCUtils.closeConnection(con, null);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
